package avicit.bdp.dms.dss.rest;

import avicit.bdp.dms.dss.dto.DataResourceDTO;
import avicit.bdp.dms.dss.dto.DssLogDTO;
import avicit.bdp.dms.dss.dto.DssSubscribeDTO;
import avicit.bdp.dms.dss.service.DataResourceService;
import avicit.bdp.dms.dss.service.DssLogService;
import avicit.bdp.dms.dss.service.DssSubscribeService;
import avicit.platform6.api.system.ConvertColumnClient;
import avicit.platform6.api.system.impl.SystemConstant;
import avicit.platform6.commons.utils.BusinessUtil;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *数据资源日志接口
*/
@RestController
@RequestMapping("/api/bdp/dms/dss/log")
@Api(tags = "数据资源日志接口")
public class DssLogRest {

 private static final Logger logger = LoggerFactory.getLogger(DataResourceRest.class);

 @Autowired
 private DataResourceService dataResourceService;

 @Autowired
 private DssSubscribeService dssSubscribeService;

 @Autowired
 private DssLogService dssLogService;

 @Autowired
 private ConvertColumnClient convertColumnClient;

 @ApiOperation(value = "查询数据资源列表")
 @ApiImplicitParams({
         @ApiImplicitParam(value = "分类id", name = "typeId", dataType = "String"),
         @ApiImplicitParam(value = "查询关键字", name = "keyWords", dataType = "String"),
         @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
         @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
 })
 @GetMapping("/getDataResourceList")
 public ResponseMsg<QueryRespBean<DataResourceDTO>> getDataResourceList(@RequestParam String typeId,
                                                                        @RequestParam(required = false) String keyWords,
                                                                        @RequestParam(defaultValue = "1", required = false) Integer pageNo,
                                                                        @RequestParam(defaultValue = "10", required = false) Integer pageSize) throws Exception {
  ResponseMsg<QueryRespBean<DataResourceDTO>> responseMsg = new ResponseMsg<>();

  List<Integer> statusList = new ArrayList<>();
  statusList.add(1);
  statusList.add(2);
  QueryRespBean<DataResourceDTO> queryRespBean = dataResourceService.getDataResourceList(typeId, null,
          keyWords, null, statusList, pageNo, pageSize);
  valueDataResourceConvert(queryRespBean.getResult());
  responseMsg.setResponseBody(queryRespBean);
  return responseMsg;
 }

 /**
  * 查询资源订阅人信息
  */
 @GetMapping("/subscribeUsers")
 @ApiOperation(value = "查询资源订阅人信息")
 @ApiImplicitParams({
         @ApiImplicitParam(value = "资源id", name = "resourceId", dataType = "String"),
         @ApiImplicitParam(value = "姓名", name = "keyWords", dataType = "String"),
         @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
         @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
 })
 public ResponseMsg<QueryRespBean<DssSubscribeDTO>> subscribeUsers(@RequestParam String resourceId,
                                                                   @RequestParam(required = false) String keyWords,
                                                                   @RequestParam(defaultValue = "1", required = false) Integer pageNo,
                                                                   @RequestParam(defaultValue = "10", required = false) Integer pageSize) throws Exception {
  ResponseMsg<QueryRespBean<DssSubscribeDTO>> responseMsg = new ResponseMsg<>();

  QueryRespBean<DssSubscribeDTO> result = dssSubscribeService.subscribeUsers(resourceId, keyWords,
          pageNo, pageSize);
  responseMsg.setResponseBody(result);
  return responseMsg;
 }


 @GetMapping("/resouceLogs")
 @ApiOperation(value = "查询日志列表")
 @ApiImplicitParams({
         @ApiImplicitParam(value = "资源id", name = "resourceId", dataType = "String"),
         @ApiImplicitParam(value = "开始时间", name = "startDate", dataType = "String"),
         @ApiImplicitParam(value = "结束时间", name = "endDate", dataType = "String"),
         @ApiImplicitParam(value = "调用人姓名", name = "callUserName", dataType = "String"),
         @ApiImplicitParam(value = "类型", name = "type", dataType = "Int"),
         @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
         @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
 })
 public ResponseMsg<QueryRespBean<DssLogDTO>> resouceLogs(@RequestParam String resourceId,
                                                          @RequestParam(required = false) String startDate,
                                                          @RequestParam(required = false) String endDate,
                                                          @RequestParam(required = false) String callUserName,
                                                          @RequestParam(required = false) Integer type,
                                                          @RequestParam(defaultValue = "1", required = false) Integer pageNo,
                                                          @RequestParam(defaultValue = "10", required = false) Integer pageSize) throws Exception {
  ResponseMsg<QueryRespBean<DssLogDTO>> responseMsg = new ResponseMsg<>();

  QueryRespBean<DssLogDTO> result = dssLogService.resourceLogs(resourceId,
          startDate, endDate, callUserName, type, pageNo, pageSize);
  responseMsg.setResponseBody(result);
  return responseMsg;
 }


 /**
  * 通过平台API将字段值转换为名称，包括通用选择组件、通用代码
  */
 private void valueDataResourceConvert(List<DataResourceDTO> dataResourceDTOS) {
  //循环组装请求数据
  Map<String, Set<String>> convertFormData = new HashMap<>();
  if (CollectionUtils.isNotEmpty(dataResourceDTOS)) {
   for (DataResourceDTO dataResourceDTO : dataResourceDTOS) {
    BusinessUtil.createConvertSet(convertFormData, SystemConstant.USER, dataResourceDTO.getCreatedBy());
   }
   if (convertFormData.size() > 0) {
    //获取请求结果
    Map<String, Map<String, String>> convertResultData = convertColumnClient.replace(convertFormData);
    for (DataResourceDTO dataResourceDTO : dataResourceDTOS) {
     dataResourceDTO.setCreateUser(BusinessUtil.convertFormat(convertResultData, SystemConstant.USER,
             dataResourceDTO.getCreatedBy()));
    }
   }
  }

 }
}
